import {
  IsString,
  IsNotEmpty,
  IsOptional,
  IsNumber,
  IsBoolean,
  IsEnum,
  IsArray,
  IsDateString,
  MaxLength,
  Min,
  Max,
  Matches
} from 'class-validator';
import { ApiProperty, ApiPropertyOptional, PartialType } from '@nestjs/swagger';
import { Type } from 'class-transformer';

// 创建公告 DTO
export class CreateAnnouncementDto {
  @ApiProperty({
    description: '公告标题',
    example: '系统维护通知',
    maxLength: 255
  })
  @IsString()
  @IsNotEmpty()
  @MaxLength(255)
  title: string;

  @ApiProperty({
    description: '公告内容',
    example: '系统将于今晚 22:00-24:00 进行维护，请提前保存工作...'
  })
  @IsString()
  @IsNotEmpty()
  content: string;

  @ApiProperty({
    description: '公告类型',
    example: '系统',
    enum: ['系统', '活动', '通知']
  })
  @IsEnum(['系统', '活动', '通知'])
  type: '系统' | '活动' | '通知';

  @ApiPropertyOptional({
    description: '优先级',
    example: 2,
    enum: [1, 2, 3],
    default: 2
  })
  @IsOptional()
  @IsEnum([1, 2, 3])
  priority?: number;

  @ApiPropertyOptional({
    description: '状态',
    example: 1,
    enum: [0, 1],
    default: 1
  })
  @IsOptional()
  @IsEnum([0, 1])
  status?: number;

  @ApiPropertyOptional({
    description: '是否置顶',
    example: false,
    default: false
  })
  @IsOptional()
  @IsBoolean()
  isTop?: boolean;

  @ApiPropertyOptional({
    description: '发布时间',
    example: '2024-01-01T00:00:00.000Z'
  })
  @IsOptional()
  @IsDateString()
  publishedAt?: string;

  @ApiPropertyOptional({
    description: '过期时间',
    example: '2024-12-31T23:59:59.999Z'
  })
  @IsOptional()
  @IsDateString()
  expiredAt?: string;

  @ApiPropertyOptional({
    description: '阅读量',
    example: 100,
    default: 0
  })
  @IsOptional()
  @IsNumber()
  @Min(0)
  viewCount?: number;

  @ApiPropertyOptional({
    description: '作者ID',
    example: 1
  })
  @IsOptional()
  @IsNumber()
  authorId?: number;

  @ApiPropertyOptional({
    description: '作者名称',
    example: 'Admin',
    maxLength: 100
  })
  @IsOptional()
  @IsString()
  @MaxLength(100)
  authorName?: string;

  @ApiPropertyOptional({
    description: '封面图',
    example: 'https://example.com/cover.jpg',
    maxLength: 500
  })
  @IsOptional()
  @IsString()
  @MaxLength(500)
  cover?: string;

  @ApiPropertyOptional({
    description: '公告摘要'
  })
  @IsOptional()
  @IsString()
  summary?: string;

  @ApiPropertyOptional({
    description: '标签',
    type: [String],
    example: ['重要', '系统']
  })
  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  tags?: string[];

  @ApiPropertyOptional({
    description: '是否允许评论',
    example: true,
    default: true
  })
  @IsOptional()
  @IsBoolean()
  allowComment?: boolean;

  @ApiPropertyOptional({
    description: '颜色',
    example: '#1890ff',
    default: '#1890ff'
  })
  @IsOptional()
  @IsString()
  @Matches(/^#[0-9A-Fa-f]{6}$/, {
    message: '颜色格式应为 #RRGGBB'
  })
  color?: string;
}

// 更新公告 DTO
export class UpdateAnnouncementDto extends PartialType(CreateAnnouncementDto) {}

// 查询公告 DTO
export class QueryAnnouncementsDto {
  @ApiPropertyOptional({
    description: '页码',
    example: 1,
    minimum: 1,
    default: 1
  })
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  @Min(1)
  page?: number = 1;

  @ApiPropertyOptional({
    description: '每页数量',
    example: 10,
    minimum: 1,
    maximum: 100,
    default: 10
  })
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  @Min(1)
  @Max(100)
  limit?: number = 10;

  @ApiPropertyOptional({
    description: '搜索关键词（标题、内容）',
    example: '维护'
  })
  @IsOptional()
  @IsString()
  search?: string;

  @ApiPropertyOptional({
    description: '公告类型',
    enum: ['系统', '活动', '通知']
  })
  @IsOptional()
  @IsEnum(['系统', '活动', '通知'])
  type?: '系统' | '活动' | '通知';

  @ApiPropertyOptional({
    description: '优先级',
    enum: [1, 2, 3]
  })
  @IsOptional()
  @Type(() => Number)
  @IsEnum([1, 2, 3])
  priority?: number;

  @ApiPropertyOptional({
    description: '状态',
    enum: [0, 1]
  })
  @IsOptional()
  @Type(() => Number)
  @IsEnum([0, 1])
  status?: number;

  @ApiPropertyOptional({
    description: '是否置顶',
    example: true
  })
  @IsOptional()
  @Type(() => Boolean)
  @IsBoolean()
  isTop?: boolean;

  @ApiPropertyOptional({
    description: '作者ID',
    example: 1
  })
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  authorId?: number;

  @ApiPropertyOptional({
    description: '是否包含过期的公告',
    example: false,
    default: false
  })
  @IsOptional()
  @Type(() => Boolean)
  @IsBoolean()
  includeExpired?: boolean;

  @ApiPropertyOptional({
    description: '排序字段',
    example: 'createdAt',
    enum: ['createdAt', 'updatedAt', 'publishedAt', 'title', 'priority', 'viewCount']
  })
  @IsOptional()
  @IsString()
  sortBy?: string = 'createdAt';

  @ApiPropertyOptional({
    description: '排序方向',
    example: 'DESC',
    enum: ['ASC', 'DESC'],
    default: 'DESC'
  })
  @IsOptional()
  @IsString()
  sortOrder?: 'ASC' | 'DESC' = 'DESC';
}

// 批量操作公告 DTO
export class BatchAnnouncementActionDto {
  @ApiProperty({
    description: '公告ID数组',
    type: [Number],
    example: [1, 2, 3]
  })
  @IsArray()
  @IsNumber({}, { each: true })
  announcementIds: number[];

  @ApiProperty({
    description: '操作类型',
    enum: ['publish', 'unpublish', 'top', 'untop', 'delete', 'changeType']
  })
  @IsString()
  @IsEnum(['publish', 'unpublish', 'top', 'untop', 'delete', 'changeType'])
  action: 'publish' | 'unpublish' | 'top' | 'untop' | 'delete' | 'changeType';

  @ApiPropertyOptional({
    description: '目标类型（用于更改类型操作）',
    enum: ['系统', '活动', '通知']
  })
  @IsOptional()
  @IsEnum(['系统', '活动', '通知'])
  targetType?: '系统' | '活动' | '通知';
}
